/*
 *  Emotion Media Center
 *
 *  Copyright (C) 2009 Davide Andreoli <dave@gurumeditation.it>
 *
 *  License LGPL-2.1, see COPYING file at project folder.
 *
 */

#include "emc.h"
#include "emc_module.h"
#include "emc_input.h"

#define EMC_MODULE_NAME "input_keyb"

#define LOCAL_DEBUG 0
#if LOCAL_DEBUG
#define D DBG
#else
#define D(...)
#endif


static const struct
{
   const char *keyname;
   Ecore_Event_Modifier modifier;
   emc_input input;
} emc_keymap[] = {
   { "Left",         0,              EMC_INPUT_LEFT          },
   { "Right",        0,              EMC_INPUT_RIGHT         },
   { "Up",           0,              EMC_INPUT_UP            },
   { "KP_Up",        0,              EMC_INPUT_UP            },
   { "Down",         0,              EMC_INPUT_DOWN          },
   { "KP_Down",      0,              EMC_INPUT_DOWN          },
   { "Home",         0,              EMC_INPUT_HOME          },
   { "KP_Home",      0,              EMC_INPUT_HOME          },
   { "End",          0,              EMC_INPUT_END           },
   { "KP_End",       0,              EMC_INPUT_END           },
   { "Prior",        0,              EMC_INPUT_PREV          },
   { "Next",         0,              EMC_INPUT_NEXT          },
   { "Return",       0,              EMC_INPUT_OK            },
   { "KP_Enter",     0,              EMC_INPUT_OK            },
   { "space",        0,              EMC_INPUT_PAUSE         },
   { "BackSpace",    0,              EMC_INPUT_EXIT          },
   { "Escape",       0,              EMC_INPUT_EXIT          },
   { "Super_L",      0,              EMC_INPUT_MENU          },
   { "Meta_L",       0,              EMC_INPUT_MENU          },
   { "Hyper_L",      0,              EMC_INPUT_MENU          },
   { "m",            0,              EMC_INPUT_MENU          },
   { "plus",         0,              EMC_INPUT_VOLUMEUP      },
   { "KP_Add",       0,              EMC_INPUT_VOLUMEUP      },
   { "minus",        0,              EMC_INPUT_VOLUMEDOWN    },
   { "KP_Subtract",  0,              EMC_INPUT_VOLUMEDOWN    },
   { "f",            0,              EMC_INPUT_FULLSCREEN    },
   { "q",            0,              EMC_INPUT_QUIT          },
   { "0",            0,              EMC_INPUT_KEY_0         },
   { "KP_0",         0,              EMC_INPUT_KEY_0         },
   { "1",            0,              EMC_INPUT_KEY_1         },
   { "KP_1",         0,              EMC_INPUT_KEY_1         },
   { "2",            0,              EMC_INPUT_KEY_2         },
   { "KP_2",         0,              EMC_INPUT_KEY_2         },
   { "3",            0,              EMC_INPUT_KEY_3         },
   { "KP_3",         0,              EMC_INPUT_KEY_3         },
   { "4",            0,              EMC_INPUT_KEY_4         },
   { "KP_4",         0,              EMC_INPUT_KEY_4         },
   { "5",            0,              EMC_INPUT_KEY_5         },
   { "KP_5",         0,              EMC_INPUT_KEY_5         },
   { "6",            0,              EMC_INPUT_KEY_6         },
   { "KP_6",         0,              EMC_INPUT_KEY_6         },
   { "7",            0,              EMC_INPUT_KEY_7         },
   { "KP_7",         0,              EMC_INPUT_KEY_7         },
   { "8",            0,              EMC_INPUT_KEY_8         },
   { "KP_8",         0,              EMC_INPUT_KEY_8         },
   { "9",            0,              EMC_INPUT_KEY_9         },
   { "KP_9",         0,              EMC_INPUT_KEY_9         },
   /*{ "a",            0,              EMC_INPUT_KEY_A         },
   { "b",            0,              EMC_INPUT_KEY_B         },
   { "c",            0,              EMC_INPUT_KEY_C         },
   { "d",            0,              EMC_INPUT_KEY_D         },
   { "e",            0,              _INPUT_KEY_E         },
   { "f",            0,              _INPUT_KEY_F         },
   { "g",            0,              _INPUT_KEY_G         },
   { "h",            0,              _INPUT_KEY_H         },
   { "i",            0,              _INPUT_KEY_I         },
   { "j",            0,              _INPUT_KEY_J         },
   { "k",            0,              _INPUT_KEY_K         },
   { "l",            0,              _INPUT_KEY_L         },
   { "m",            0,              _INPUT_KEY_M         },
   { "n",            0,              _INPUT_KEY_N         },
   { "o",            0,              _INPUT_KEY_O         },
   { "p",            0,              _INPUT_KEY_P         },
   { "q",            0,              _INPUT_KEY_Q         },
   { "r",            0,              _INPUT_KEY_R         },
   { "s",            0,              _INPUT_KEY_S         },
   { "t",            0,              _INPUT_KEY_T         },
   { "u",            0,              _INPUT_KEY_U         },
   { "v",            0,              _INPUT_KEY_V         },
   { "w",            0,              _INPUT_KEY_W         },
   { "x",            0,              _INPUT_KEY_X         },
   { "y",            0,              _INPUT_KEY_Y         },
   { "z",            0,              _INPUT_KEY_Z         },*/
   { NULL,           0,              EMC_INPUT_UNKNOWN    }
};


static Ecore_Event_Handler *_event_handler;

static emc_input
_get_input_from_event(Ecore_Event_Key *ev)
{
   int i;

   if (!ev) return EMC_INPUT_UNKNOWN;

   for (i = 0; emc_keymap[i].keyname; i++)
   {
      /* Test First if modifer is set and is different than "None"*/
      if (emc_keymap[i].modifier &&
         ev->modifiers == emc_keymap[i].modifier &&
         !strcmp(emc_keymap[i].keyname, ev->key))
      {
         return emc_keymap[i].input;
      }
      /* Else test if keyname match */
      else if (!strcmp (emc_keymap[i].keyname, ev->key) &&
               !emc_keymap[i].modifier)
      {
         return emc_keymap[i].input;
      }
   }

   return EMC_INPUT_UNKNOWN;
}

static int
_ecore_event_key_down_cb(void *data, int type, void *event)
{
   Ecore_Event_Key *e = event;
   emc_input in;

   D(" ");

   in = _get_input_from_event(e);
   if (in != EMC_INPUT_UNKNOWN)
      emc_input_event_emit(in);

   return ECORE_CALLBACK_CANCEL;
}


/* Module interface */

Eina_Bool
module_init(Emc_Module *em)
{
   D(" ");
   _event_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
                                            _ecore_event_key_down_cb, NULL);

   return _event_handler ? EINA_TRUE : EINA_FALSE;
}

Eina_Bool
module_shutdown(Emc_Module *em)
{
   D(" ");
   EMC_EVENT_HANDLER_DEL(_event_handler);
   return EINA_TRUE;
}

Emc_Module_Api module_api =
{
   EMC_MODULE_API_VERSION,
   EMC_MODULE_NAME,
   "Keyboard controls",
   NULL,
   "Module to control enna from the keyboard",
   "bla bla bla<br><b>bla bla bla</b><br><br>bla.",
   {
      module_init,
      module_shutdown
   }
};
